﻿package com.d4p.util
{
	public class Operator
	{
		static private function random():Number{
			return Math.random();	
		}
		
		static private function getDigit(min:Number, max:Number, digit:uint):uint{
			var minA:Array = String(min).split(".")
			var maxA:Array = String(min).split(".")
			var minD:uint = (minA.length >1) ? String(minA[1]).length : new uint(0);
			var maxD:uint = (maxA.length >1) ? String(maxA[1]).length : new uint(0);
			var result:uint = Math.max(minD, maxD , digit);
			return result;
		}

		
		
		static public function randomNumber(min:Number, max:Number, digit:uint=0):Number{
			var result:Number;
			var digit:uint = Operator.getDigit(min, max, digit);
			do{
				var r:Number = Operator.random();
			}while( r == min);
			result = min + r * (max - min);
			result = Number(result.toFixed(digit));
			return result;
		}
		
		static public function randomInt(min:int, max:int):int{
			var result:int = int(Operator.randomNumber(min, max,0));
			return result
		}
		
		static public function randomUint(min:uint , max:uint):uint{
			var result:uint = uint(Operator.randomNumber(min, max,0));
			return result;
		}
		
		
		/// convert current value to percent value
		static public function valueToPercent(value:Number, maxValue:Number):Number
		{
			var result:Number = value / maxValue * 100;
	    	return (isNaN(result) || !isFinite(result)) ? NaN : result ;

		}
		
		/// convert current Percent value to point value
		static public function percentToValue(percent:Number = NaN, maxValue:Number = NaN):Number
		{
			var result:Number = maxValue * percent / 100;
			return (isNaN(result) || !isFinite(result)) ? NaN : result ;
		}
	}
}
